package edu.zju.gis.dldsj.utils;

import edu.zju.gis.dldsj.config.CommonSetting;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

import java.io.File;
import java.io.IOException;

public final class ZipCompressorByAnt {

    private File zipFile;
    private CommonSetting setting;

    public ZipCompressorByAnt(String finalFile) {
        zipFile = new File(finalFile);
        setting = SpringUtil.getBean(CommonSetting.class);
        System.out.println("============= 1 =============");
    }

    public void compressExe(String srcPathName, String filename, boolean isCompress) {
        File srcdir = new File(srcPathName);
        System.out.println("============== 2 ============");
        if (!srcdir.exists()) {
            System.out.println(srcPathName);
            System.out.println("============== 3 ============");
            throw new RuntimeException(srcPathName + "不存在！");
        }
        if (isCompress) {
            compressExe(srcPathName, filename);
        } else {
            // 利用SSH调用不压缩的tar命令
            StringBuilder cmd = new StringBuilder();
            cmd.append("cd ").append(srcPathName).append(";");
            cmd.append("tar -cf ").append(this.zipFile.getAbsolutePath()).append(" ").append(filename).append(".*");
            System.out.println("============= 4 =============");
            System.out.println(cmd.toString());
            try {
                SSHHelper.runSSHWithOutput(setting.getNameNode(), setting.getUsername(), setting.getPassword(),
                        cmd.toString(), setting.getParallelFilePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void compressExe(String srcPathName, String filename) {
        File srcdir = new File(srcPathName);
        if (!srcdir.exists()) {
            throw new RuntimeException(srcPathName + "不存在！");
        }

        Project prj = new Project();
        Zip zip = new Zip();
        zip.setProject(prj);
        zip.setEncoding("gb2312");
        zip.setDestFile(zipFile);
        FileSet fileSet = new FileSet();
        fileSet.setProject(prj);
        fileSet.setDir(srcdir);
        fileSet.setIncludes(filename + ".*"); //包括哪些文件或文件夹
        // eg:zip.setIncludes("*.java");
        fileSet.setExcludes(filename + ".zip"); //排除哪些文件或文件夹
        zip.addFileset(fileSet);
        zip.execute();
    }

    public void compressExe(String fileOrDir) {
        File file = new File(fileOrDir);
        if (!file.exists())
            throw new RuntimeException(fileOrDir + "不存在！");

        Project prj = new Project();
        Zip zip = new Zip();
        zip.setProject(prj);
        zip.setCompress(false);
        //zip.setKeepCompression(false);
        zip.setEncoding("gb2312");
        zip.setDestFile(zipFile);
        FileSet fileSet = new FileSet();
        fileSet.setProject(prj);
        if (file.isFile())
            fileSet.setFile(file);
        else
            fileSet.setDir(file);
        // fileSet.setIncludes(filename+".*"); //包括哪些文件或文件夹
        // eg:zip.setIncludes("*.java");

        zip.addFileset(fileSet);
        zip.execute();
    }

    public void compressExe(String srcPathName, boolean isCompress) {

        File srcdir = new File(srcPathName);
        if (!srcdir.exists()) {
            throw new RuntimeException(srcPathName + "不存在！");
        }

        if (isCompress) {
            compressExe(srcPathName);
        } else {
            // 利用SSH调用不压缩的tar命令
            StringBuilder cmd = new StringBuilder();
            cmd.append("cd ").append(srcPathName).append(";");
            cmd.append("tar -cf ").append(this.zipFile.getAbsolutePath()).append(" ./* && echo good");
            // System.out.println(cmd.toString());
            try {
                SSHHelper.runSSHWithOutput(setting.getNameNode(), setting.getUsername(), setting.getPassword(),
                        cmd.toString(), setting.getParallelFilePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
